
package tests;

import java.util.*;
import java.util.logging.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
//import org.springframework.transaction.annotation.Transactional;

import cz.czu.pef.DataModelling.Address;
import cz.czu.pef.DataModelling.dao.AddressDao;
import cz.czu.pef.DataModelling.data.BasicGardeningData;
import cz.czu.pef.DataModelling.utils.Constants;

@Component(value="dataModellingTester")
@Transactional
public class DataModellingTester
{
	public static Logger logger = Logger.getLogger(DataModellingTester.class.getName());

	@Autowired BasicGardeningData basicGardeningData;
	ApplicationContext applicationContext;

	public static void main(String[] args)
	{
		(new DataModellingTester()).doTest();
	}

	private void doTest()
	{
		try
		{
			logger.log(Level.INFO, "doTest():: Entry");
			applicationContext = new ClassPathXmlApplicationContext(Constants.springTestingClasspathConfig);
			AddressDao addressDao = (AddressDao) applicationContext.getBean("addressDao");
			List<Address> addresses = addressDao.findAll();
			System.out.println(addresses);

			/*
		  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		  ctx.scan("cz.czu.pef");
		  ctx.refresh();
		  basicGardeningData fillBasicGardeningData = ctx.getBean(basicGardeningData.class);
		  */

			//EntityManagerFactory emf = (EntityManagerFactory)applicationContext.getBean("entityManagerFactory");
		  //entityManager = emf.createEntityManager();

			basicGardeningData = (BasicGardeningData)applicationContext.getBean("fillBasicGardeningData");
		  //applicationContext.getAutowireCapableBeanFactory().autowireBean(fillBasicGardeningData);


		  //entityManager.getTransaction().begin();
			basicGardeningData.fillBasicData();
			//entityManager.getTransaction().commit();

		  //entityManager.getTransaction().begin();
			basicGardeningData.fillPeopleAndCompanies();
			//entityManager.getTransaction().commit();

		  //entityManager.getTransaction().begin();
			basicGardeningData.fillMachineries();
			//entityManager.getTransaction().commit();
		}
		catch (InstantiationException e)
		{
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}
		finally
		{
			logger.log(Level.INFO, "doTest():: Exit");
		}
	}
}
